package com.wgj.android.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.adapter.AdverAdapter;
import com.wgj.android.dialog.AdverPictureDialog;
import com.wgj.android.dialog.AdverViedoDialog;
import com.wgj.android.dialog.OpenAdverHongBaoDialog;
import com.wgj.android.events.AdaverHongBaoEvent;
import com.wgj.android.events.Adver;
import com.wgj.android.http.WGJAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GuanggaoActivity extends AppCompatActivity {
    private ArrayList<Adver> advers = new ArrayList<>();
    @BindView(R.id.advList)
    RecyclerView advList;
    private AdverAdapter adverAdApter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guanggao1);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        advList.setHasFixedSize(true);
        advList.setLayoutManager(new LinearLayoutManager(this));
        adverAdApter=new AdverAdapter(this);
        adverAdApter.setOnItemClickListener(new AdverAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(advers.get(position).getType()==1){
                    AdverPictureDialog.getInstael(advers.get(position)).show(getSupportFragmentManager(),"AdverViedoDialog");
                //    startActivity(new Intent(GuanggaoActivity.this,PictureActivity.class).putExtra("imgs", advers.get(position).getImgs()));
                    //PictureDialog.getInstael(advers.get(position).getImgs()).show(getSupportFragmentManager(),"dialog");
                }else {
                    AdverViedoDialog.getInit(advers.get(position)).show(getSupportFragmentManager(),"AdverViedoDialog");
                }
            }
        });
        advList.setAdapter(adverAdApter);
        getData();

    }
    private  void getData(){
        XHttp.obtain().get(WGJAPI.getAdaverAll(), null, new ListHttpCallBack<Adver>() {
            @Override
            public void onSuccess(List<Adver> as) {

                if(as.size()>0){
                    advers= (ArrayList<Adver>) as;
                    adverAdApter.update(as);
                }else {
                    XToast.info("暂无无数据");
                }
            }
            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.info("异常");
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public  void onEvents(AdaverHongBaoEvent event){
         if(event.isOpen()){
             adverAdApter.notifyDataSetChanged();
         }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void onResume() {
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        super.onResume();
    }
}
